package com.oplink.webbase.modules.cfg.services;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.oplink.webbase.common.persistence.Page;
import com.oplink.webbase.modules.cfg.dao.CfgCardDao;
import com.oplink.webbase.modules.cfg.dao.CfgSiteDao;
import com.oplink.webbase.modules.cfg.entity.CfgCard;
import com.oplink.webbase.modules.cfg.entity.CfgNe;
import com.oplink.webbase.modules.cfg.entity.CfgSite;
import com.oplink.webbase.modules.cfg.entity.CfgSlot;
import com.oplink.webbase.modules.sys.dto.MsgDTO;
import com.oplink.webbase.modules.sys.dto.RespDTO;

/**
 * 
 * Title: 板卡配置服务
 * Description:
 * Copyright: Copyright (c) 2014
 * Company: Oplink Communications
 * 
 * @author
 * @serialData 2014-9-23 上午8:39:26
 * @version 1.0
 */
@Component
@Transactional
public class CfgCardService {

	@Autowired
	private CfgCardDao cfgDao;

	@Autowired
	private CfgSiteDao siteDao;

	public Page<CfgCard> find(Page<CfgCard> page, CfgCard card) {

		DetachedCriteria dc = cfgDao.createDetachedCriteria();
		// 添加查询条件
		if (StringUtils.isNotEmpty(card.getCardname())) {
			dc.add(Restrictions.like("cardname", "%" + card.getCardname() + "%"));
		}
		/* dc.add(Restrictions.eq(Index.DEL_FLAG, Index.DEL_FLAG_NORMAL)); */
		dc.addOrder(Order.desc("id"));
		return cfgDao.find(page, dc);
	}

	public List<CfgSite> getAllSite() {
		return (List<CfgSite>) siteDao.findAll();
	}

	public List<CfgSlot> getAll() {
		Iterable<CfgCard> itbl = cfgDao.findAll();
		Iterator<CfgCard> iter = itbl.iterator();
		List<CfgSlot> listOut = new ArrayList<CfgSlot>();
		while (iter.hasNext()) {
			CfgCard card = iter.next();
			CfgSlot slotCard = new CfgSlot();
			slotCard.setCard(card);
			listOut.add(slotCard);
		}
		return listOut;
	}

	private CfgSlot getSlotBySid(Long sid, List<CfgSlot> listSlot) {
		for (CfgSlot slot : listSlot) {
			if (slot.getNum() == sid) {
				return slot;
			}
		}
		return null;
	}

	/**
	 * 新增板卡
	 * 
	 * @param nid 网元id
	 * @param sid 槽位id
	 * @param ctype 板卡类型
	 * @param neService ne服务
	 * @param snmpService snmp服务
	 * @return 返回显示对象
	 */
	public RespDTO addCard(Long nid, Long sid, String ctype, CfgNeService neService) {
		RespDTO rsp = new RespDTO(RespDTO.ERROR, MsgDTO.ERR_NOT_FOUND_CARD_TYPE);
		try {
			CfgNe ne = neService.getCfgNe(nid);
			if (ne != null) {
				CfgSlot slot = getSlotBySid(sid, ne.getSlotlist());
				if (slot != null) {
					CfgCard card = getCardByType(ctype);
					if (card != null) {
						// zengyong -- 创建远程板卡
						CfgCard remoteCard = new CfgCard();
						List<CfgSlot> rLst = new ArrayList<CfgSlot>();
						CfgSlot rSlot = new CfgSlot();
						rSlot.setNum(sid.intValue());
						rLst.add(rSlot);
						remoteCard.setSlotlist(rLst);
						remoteCard.setType(ctype);
						// 将板卡保存到数据库
						slot.setCard(card);
						neService.save(ne);
						// 提交远程处理
						// Card snmpCard = CardParser.toSnmpCard(remoteCard);
						// boolean r = snmpService.addCard(snmpCard);
						// if (r) {
						rsp.setResult(RespDTO.SUCCESS);
						rsp.setMessage(MsgDTO.OK_ADD_CARD_SUCCESS);
						// } else {
						// rsp.setResult(RespDTO.ERROR);
						// rsp.setMessage(MsgDTO.ERR_DEL_DEVICE_CARD);
						// }
					} else {
						rsp.setResult(RespDTO.ERROR);
						rsp.setMessage(MsgDTO.ERR_QUERY_DB_CARD);
					}
				} else {
					rsp.setResult(RespDTO.ERROR);
					rsp.setMessage(MsgDTO.ERR_QUERY_DB_SLOT);
				}
			} else {
				rsp.setResult(RespDTO.ERROR);
				rsp.setMessage(MsgDTO.ERR_QUERY_DB_NE);
			}
			return rsp;
		} catch (Exception e) {
			rsp.setResult(RespDTO.ERROR);
			// if (e instanceof SnmpException) {
			// rsp.setMessage(((SnmpException) e).getCode());
			// } else {
			rsp.setMessage(e.getCause().getLocalizedMessage());
			// }
			return rsp;
		}
	}

	/**
	 * 删除板卡
	 * 
	 * @param nid
	 * @param sid
	 * @param neService
	 * @param snmpService
	 * @return 返回对象
	 */
	public RespDTO delCard(Long nid, Long sid, CfgNeService neService) {
		RespDTO rest = new RespDTO(RespDTO.ERROR, MsgDTO.ERR_SAVE_FAILURE);
		try {
			CfgNe ne = neService.getCfgNe(nid);
			if (ne != null) {
				CfgSlot slot = getSlotBySid(sid, ne.getSlotlist());
				if (slot != null) {
					if (slot.getCard() != null) {
						try {
							// Card snmpCard = CardParser.toSnmpCard(slot.getCard());
							// boolean r = snmpService.delCard(snmpCard);
							// if (r) {
							rest.setResult(RespDTO.SUCCESS);
							rest.setMessage(MsgDTO.OK_DEL_CARD_SUCCESS);
							// } else {
							// rest.setResult(RespDTO.ERROR);
							// rest.setMessage(MsgDTO.ERR_DEL_DEVICE_CARD);
							// }
						} catch (Exception e) {
							e.printStackTrace();
							rest.setResult(RespDTO.ERROR);
							rest.setMessage(e.getMessage());
						}
						CfgCard delCard = new CfgCard();
						delCard.setSlotlist(slot.getCard().getSlotlist());
						delCard.setType(slot.getCard().getType());
						// 去掉关联关系
						slot.setCard(null);
						neService.save(ne);
					} else {
						rest.setResult(RespDTO.SUCCESS);
						rest.setMessage(MsgDTO.ERR_QUERY_DB_CARD);
					}
				} else {
					rest.setResult(RespDTO.ERROR);
					rest.setMessage(MsgDTO.ERR_QUERY_DB_SLOT);
				}
			} else {
				rest.setResult(RespDTO.ERROR);
				rest.setMessage(MsgDTO.ERR_QUERY_DB_NE);
			}
			return rest;
		} catch (Exception e) {
			// if (e instanceof SnmpException) {
			// rest.setMessage(((SnmpException) e).getSnmpCause());
			// } else {
			rest.setMessage(e.getCause().getLocalizedMessage());
			// }
			return rest;
		}
	}

	public CfgCard getCardByType(String type) {
		DetachedCriteria dc = cfgDao.createDetachedCriteria();
		// 添加查询条件
		if (StringUtils.isNotEmpty(type)) {
			dc.add(Restrictions.eq("type", type));
		}
		List<CfgCard> listCards = cfgDao.find(dc);
		if ((listCards != null) && (listCards.size() > 0)) {
			return listCards.get(0);
		}
		return null;
	}

	public CfgCard getCfgCard(Long id) {
		return cfgDao.findOne(id);
	}

	public void saveCfgCard(CfgCard entity) {
		cfgDao.save(entity);
	}

	public void deleteCfgCard(Long id) {
		cfgDao.delete(id);
	}

	@Autowired
	public void setCfgCardDao(CfgCardDao cfgDao) {
		this.cfgDao = cfgDao;
	}
}
